# coding:utf-8
# @Time    : 2021/11/29 17:38
# @Author  : Martin
from page_object.base import PageItems
from util.read_file import get_approve_data


class FreeCombinePackage(PageItems):
    _base_url = "/nspas-web-in/processRoleFilter.do?businessTemplateId=NSOA23388&comType=1"

    def _select_package_type(self, txt):
        """
        选择子商品池类型
        :param txt: 定价到子商品池 定价到商品
        :return:
        """
        self.select('id', 'isPriceCommodiy', txt, 'text')

    def input_price_goods(self, params):
        """
        输入定价到商品
        :param params:
        :return:
        """
        self._select_package_type('定价到商品')
        self.switch_alert()
        for param in params:
            # 点击新增规则
            self.click('xpath', '//a[@onclick="freePackageRuleAdd()"]')
            # 切换到子窗口
            self.switch_current_handle()
            # 输入子商品池名称
            self.input('id', 'ruleName', param['rule_name'])
            # 输入可选池内商品数量
            self.input('id', 'commodityNum', param['rule_num'])

            self.input_single_goods(param['sub_goods'])

    def input_single_goods(self, params):
        """
        手工维护单商品
        :param params:商品列表
        :return:
        """
        # 点击增加（单独维护）
        self.click('xpath', '//a[@onclick="showSingleOrBatch(\'single\')"]')
        for param in params:
            # 输入子商品活动价
            self.input('id', 'activityScopePrice', param['price'])
            # 输入商品编码
            self.input('id', 'singleScopeCode', param['code'])
            # 点击加入
            self.click('xpath', '//a[@onclick="addSingleFreeScope()"]')
        # 点击保存
        self._save_goods()
        # 点击弹窗页返回
        self._go_back()
        # 切换到主窗口
        self.switch_current_handle()

    def batch_import(self):
        pass
        # showSingleOrBatch('batch')  singleFile onclick=addBitchFreeScope()

    def _save_goods(self):
        # 子商品池页面点击保存
        self.click('id', 'evaluateSaveBtn')

    def _go_back(self):
        """
        弹窗页操作成功后返回
        :return:
        """
        self.click('xpath', '//input[@onclick="goBackFun()"]')


    @staticmethod
    def get_approve_area_create():
        """
        获取大区新建审批流账号数据
        :return:
        """
        return get_approve_data('free_combine_package', 'area', 'create')



if __name__ == '__main__':
    pass
